/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nu.zoom.catonine.swing.tail;

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

import nu.zoom.catonine.tail.Tailer;
import nu.zoom.gal.error.ErrorReporter;
import nu.zoom.gal.progress.Progress;
import nu.zoom.gal.progress.Progress.TaskID;
import nu.zoom.swing.action.AbstractTypedAction;
import nu.zoom.ui.Resources;
import nu.zoom.ui.Resources.ResourceNotFoundException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Uses the java.awt.Desktop to open the file the tailer is currently following.
 * 
 * @author "Johan Maasing" &lt;johan@zoom.nu&gt;
 * 
 */
public class ViewFileAction extends AbstractTypedAction {
	private final Log log = LogFactory.getLog(getClass());
	private static final long serialVersionUID = 1L;
	private final Tailer tailer;
	private final ErrorReporter errorReporter;
	private final Progress progress;
	private final Resources resources;

	public ViewFileAction(final Tailer tailer, final Resources resources, final ErrorReporter errorReporter,
			final Progress progress) {
		this.tailer = tailer;
		this.errorReporter = errorReporter;
		this.progress = progress;
		this.resources = resources;
		try {
			setName(resources.getMessage("nu.zoom.catonine.tail.open"));
			setToolTip(resources.getMessage("nu.zoom.catonine.tail.open.tt"));
			setIcon(resources.getIcon("nu.zoom.catonine.tail.open.icon"));
		} catch (ResourceNotFoundException e) {
		}
		if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
			log.debug("JVM Supports Desktop and Action.OPEN");
			setEnabled(true);
		} else {
			log.debug("JVM does not Support Desktop or Action.OPEN, disabling action");
			setEnabled(false);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			File file = tailer.getFile();
			if (file != null) {
				final TaskID taskID = progress.createTask();
				progress.setIndeterminate(taskID, true);
				Desktop.getDesktop().open(file);
				try {
					progress.setMessage(taskID,
							resources.format("nu.zoom.catonine.tail.file.viewed", file.getAbsolutePath()));
				} catch (ResourceNotFoundException e1) {
					log.error("Unable to find resource nu.zoom.catonine.tail.file.viewed", e1);
				}
				progress.deleteTask(taskID);
			}
		} catch (IOException exc) {
			errorReporter.reportError(exc);
		}
	}

}
